<template>
	<view class="content">
		<u--form labelPosition="left" labelWidth="70" :model="userInfo" :rules="rules" ref="form1">
			<u-form-item label="回访客户" prop="nickname" @click="show = true">
				<u--input v-model="userInfo.nickname" disabled disabledColor="#ffffff" placeholder="请选择回访客户" suffixIcon="arrow-down">
				</u--input>
			</u-form-item>
			<u-form-item label="回访内容" prop="content">
				<u--textarea v-model="userInfo.content" placeholder="请输入内容" count confirmType="done"></u--textarea>
			</u-form-item>
		</u--form>
		<u-button @click="submit" color="#19BC9C">编辑回访内容</u-button>
		<u-picker :show="show" :columns="columns" keyName="nickname" @cancel="show = false" @confirm="confirm"></u-picker>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				userInfo: {
					content: '',
					busid: null,
					nickname: ''
				},
				columns: [],
				rules: {
					'content': {
						type: 'string',
						required: true,
						message: '请填写回访内容',
						trigger: ['blur', 'change']
					},
					'nickname': {
						type: 'string',
						required: true,
						message: '请选择回访客户',
						trigger: ['blur', 'change']
					},
				},
				LoginUser: {},
				businesslist: [],
				id: 0
			}
		},
		onLoad(options){
			this.id = options.id ? options.id : 0
			this.getvisitinfo()
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.form1.setRules(this.rules)
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			this.$u.auth.check()
			
			this.getbusinessdata()
		},
		methods: {
			async getbusinessdata(){
				let result = await this.$u.api.business.businesslist({adminid:this.LoginUser.id})
				if(result.code == 1){
					this.columns.push(result.data)
				}
			},
			async getvisitinfo(){
				let result = await this.$u.api.business.info({id:this.id})
				if(result.code == 1){
					this.userInfo.busid = result.data.busid
					this.userInfo.content =  result.data.content
					this.userInfo.nickname = result.data.business.nickname
				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				this.show = false
				this.userInfo.busid = e.value[0].id
				this.userInfo.nickname = e.value[0].nickname
			},
			submit(){
				this.$refs.form1.validate().then(async(res) => {
					let data = {
						id: this.id,
						busid: this.userInfo.busid,
						adminid: this.LoginUser.id,
						content: this.userInfo.content
					}
					let result = await this.$u.api.business.edit(data)
					if(result.code == 1){
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete() {
								uni.navigateTo({
									url: '/pages/business/visit/index'
								});
							}
						})
					}  else {
						this.$refs.uToast.show({
							type: 'error',
							message: result.msg
						})
					}
				}).catch(errors => {})
				
			}
		}
	}
</script>

<style>
	.content{
		width: 90%;
		height: 100vh;
		margin: 0 auto;
	}
</style>
